package gov.pbc.gx.nn.pz.dao;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import gov.pbc.gx.nn.pz.entity.HcZdkywtz;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

@Repository
public class ZdkywtzDao extends BaseImageDao {

	private Integer PZ_CODE_ = 4;

	public ZdkywtzDao() {
		super();

	}

	public boolean isSameValue(HcZdkywtz hcZdkywtz) {
		String type = hcZdkywtz.getType();
		String pzTemplate = hcZdkywtz.getPzTemplate();
		String hql = " from HcZdkywtz where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcZdkywtz> hcZdkywtzdatas = this.query(hql, map);
		if (!hcZdkywtzdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	public void saveZdkywtz(HcZdkywtz hcZdkywtz) {
		if (!isSameValue(hcZdkywtz)) {
			hcZdkywtz.setDeptId(this.getDeptId());
			this.save(hcZdkywtz);
		} else {
			hcZdkywtz.setDeptId(this.getDeptId());
			this.update(hcZdkywtz);
		}
	}

	public HcZdkywtz selectOneByTypeAndTemplate(String type, String pzTemplate) {
		List<HcZdkywtz> hcZdkywtzs = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("pzTemplate", pzTemplate);
		if (type == null) {
			String hql = " from HcZdkywtz where deptId=:deptid and type is null and pzTemplate=:pzTemplate and invalid=false ";
			hcZdkywtzs = this.query(hql, map);
		} else {
			String hql = " from HcZdkywtz where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false ";
			map.put("type", type);
			hcZdkywtzs = this.query(hql, map);
		}
		if (!hcZdkywtzs.isEmpty()) {
			HcZdkywtz zdk=hcZdkywtzs.get(0);
			zdk.setPzDate(new Date());
			return zdk;
		}
		return null;
	}

	public Collection<HcZdkywtz> getCoByType(String type) {
		Collection<HcZdkywtz> hcZdkywtzs;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		if (type == null) {
			String hql = " from HcZdkywtz where deptId=:deptid and  invalid = false and type is null";
			hcZdkywtzs = this.query(hql, map);
		} else {
			String hql = " from HcZdkywtz where deptId=:deptid and invalid = false and type=:type";
			map.put("type", type);
			hcZdkywtzs = this.query(hql, map);
		}
		for(HcZdkywtz zdk:hcZdkywtzs){
			zdk.setPzDate(new Date());
		}
		return hcZdkywtzs;
		}

	public Collection<HcZdkywtz> getOneGroupByType() {
		String hql = " from HcZdkywtz where deptId=:deptid and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptid", this.getDeptId());
		Collection<HcZdkywtz> hcZdkywtzs = this.query(hql, param);
		for(HcZdkywtz zdk:hcZdkywtzs){
			zdk.setPzDate(new Date());
		}
		return hcZdkywtzs;
	}

	public Collection<HcZdkywtz> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		String hql = "from HcZdkywtz where deptId=:deptid and invalid=false order by pzTemplate";
		Collection<HcZdkywtz> hcZdkywtzs = this.query(hql, map);
		for(HcZdkywtz zdk:hcZdkywtzs){
			zdk.setPzDate(new Date());
		}
		return hcZdkywtzs;

	}

	public List<HcpzXY> getHcZdkywtzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	public void saveZdkywtzXY(Collection<HcpzXY> hczdkywtzxys) {
		saveXjpzXYBase(hczdkywtzxys, PZ_CODE_);
	}

	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public void deleteTemplateById1(String id) {

		this.delete(id, HcZdkywtz.class);
	}

	/**
	 * 更新模板名稱
	 * 
	 * @auther jack.zhao
	 * @param templatename
	 * @param id
	 * @return
	 */
	public boolean updateTemplateNameDao(final String templatename, final String id) {

		// 根據id得到type
		String sql1 = "from HcZdkywtz where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcZdkywtz> HcZdkywtzs1 = query(sql1, param);
		String typestr = HcZdkywtzs1.get(0).getType();

		// 根據type查找所有的pztemplate是否用相同的，
		String sql = "from HcZdkywtz where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcZdkywtz> HcZdkywtzs = this.query(sql, param1);
		for (HcZdkywtz hczdkywtz : HcZdkywtzs) {
			if (hczdkywtz.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		// 更新操作
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcZdkywtz set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}

		});

	}

	/**
	 * 更新模板類型
	 * 
	 * @auther jack.zhao
	 * @param templatetype
	 * @param id
	 * @return
	 */
	public boolean updateTemplateTypeDao(final String templatetype, final String id) {
		// 判斷類型是否有相同的

		String sql = "select count(*) from HcZdkywtz where type=:type and invalid = false ";

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		// 更新類型
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcZdkywtz where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcZdkywtz> HcZdkywtzs = query(sql, param);
				String typestr = HcZdkywtzs.get(0).getType();

				String sql1 = "from HcZdkywtz where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcZdkywtz> HcZdkywtzs1 = query(sql1, param1);
				for (HcZdkywtz hczdkywtz : HcZdkywtzs1) {
					String hql2 = "update HcZdkywtz set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", hczdkywtz.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}
}
